package com.wy2cloud.authorize.handler;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler;

public class UserLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler {
    private static Logger logger=LoggerFactory.getLogger(UserLogoutSuccessHandler.class);
	@Override
	public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
			throws IOException, ServletException {
		if(authentication!=null && authentication instanceof UsernamePasswordAuthenticationToken){ 
			//获得授权后可得到用户信息   可使用SUserService进行数据库操作
		    UserDetails user= (UserDetails) authentication.getPrincipal();
	          
	        //输出登录提示信息  
		    logger.info("用户 " + user.getUsername() + " 退出");
		}else{
		    logger.info("未登录，退出操作有误... ");
		}
		
		this.setDefaultTargetUrl("/auth");

		super.onLogoutSuccess(request, response, authentication);
	}

}
